1656. Sorting

 

Given a sequence of 32 bit signed integers. Sort this sequence, and remove all repeating elements, i.e. remove all but one copy of each number in the sequence.

 

Input. The first line contains amount of numbers n (1 ≤ n ≤ 65536) in the sequence. The next n lines contain n integers (one number per line).

 

Output. Write no more than n numbers, sorted in descending order if n is even, and in ascending order if n is odd. Each number must appear no more than once.

 

Sample input

Sample output

6
8
8
7
3
7
7

8

7

3

 

 

SOLUTION

sorting

 

Algorithm analysis

Lets sort the numbers in the array depending on the parity of n. Remove duplicate numbers using the unique function.

 

Algorithm realization

Declare the array for storing numbers.

 

vector<int> v;

 

Read the input data.

 

scanf("%d", &n);

for (i = 0; i < n; i++)

{

  scanf("%d", &a);

  v.push_back(a);

}

 

Sort the numbers depending on the parity of n.

 

if (n % 2 == 1)

  sort(v.begin(), v.end());

else

  sort(v.begin(), v.end(), greater<int>());

 

Remove the repeating elements

 

v.erase(unique(v.begin(), v.end()), v.end());

 

Print the resulting array.

 

for (i = 0; i < v.size(); i++)

  printf("%d\n", v[i]);